package com.spukmk3me.scenepack.pack0;

import java.util.Iterator;
import java.util.LinkedList;

import com.spukmk3me.video.VideoAPI;

class OrderedRenderBuffer
{
	public OrderedRenderBuffer( VideoAPI vapi )
	{
	    m_vapi = vapi;
	    m_nodes = new LinkedList<DefaultNode>();
	}
	
	public void add( DefaultNode node )
	{
		if ( node.getEntity() != null )
		{
			Iterator<DefaultNode> i = m_nodes.descendingIterator();
			int order = node.getAccumulatedAttrib().order;
			int index = m_nodes.size();
			
			while ( i.hasNext() )
			{
				if ( i.next().getAccumulatedAttrib().order <= order )
					break;
				
				--index;
			}
			
			m_nodes.add( index, node );
		}
	}
	
	public void render()
	{
		Iterator<DefaultNode> i = m_nodes.iterator();
		DefaultNode node;
		AccumulatedAttribute attr;
		
		while ( i.hasNext() )
		{
			node = i.next();
			attr = node.getAccumulatedAttrib();
			m_vapi.setMatrix( attr.matrix );
			m_vapi.setAdditionalColor( attr.acolor );
			m_vapi.setPlaneColor( attr.pcolor );
			//m_vapi.setClip( attr.clip );
			m_vapi.render( node.getEntity(), attr.animate );
		}
		
		m_nodes.clear();
	}
	
	private VideoAPI m_vapi;
	private LinkedList<DefaultNode> m_nodes;
}
